package com.cjd.common.timer

import android.os.CountDownTimer

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/10/19
 * description
 */
class TimeCountDownTimer(
    millisInFuture: Long,
    countDownInterval: Long,
    private var listener: ((residueSecond: Long, isFinished: Boolean) -> Unit)? = null
) :
    CountDownTimer(millisInFuture, countDownInterval) {

    var isFinished = false

    fun setCountDownListener(block: ((residueSecond: Long, isFinished: Boolean) -> Unit)?) {
        listener = block
    }

    override fun onFinish() {
        isFinished = true
        listener?.invoke(0, true)
    }

    override fun onTick(millisUntilFinished: Long) {
        val residueSecond = (millisUntilFinished / 1000)
        listener?.invoke(residueSecond, false)
    }
}